<!--
  功能：学生管理
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月15日 19:25:35
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='student_list'>
    <div class="btns">
      <div style="text-align:center;margin-bottom:.5em">
        <el-form inline size="small"  >
          <el-form-item>
            <el-cascader filterable style="width:400px" v-model="param.channelId" clearable :options="channels" placeholder="请选择院校渠道"
          :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-input v-model="param.realname" placeholder="学生姓名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="loadStudents">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:right">
        <el-button size="small" type="primary" @click="toImportHandler">导入</el-button>
        <el-button size="small" type="primary">导出</el-button>
      </div>
    </div>
    <el-table :data="studentData.list" size="small" v-loading="loading"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column>
        <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
        <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
        <el-table-column prop="stuNo" label="杰普学号" width="130" align="center" fixed="left"/>
        <el-table-column prop="schoolStuNo" label="学校学号" width="120" align="center" fixed="left"/>
        <el-table-column prop="gender" label="性别" width="60" align="center"/>
        <el-table-column prop="channelId" label="渠道编号" width="120" align="center"/>
        <el-table-column prop="education" label="学历" width="120" align="center"/>
        <el-table-column prop="tel" label="手机号" width="120" align="center"/>
        <el-table-column prop="qq" label="QQ号" width="120" align="center"/>
        <el-table-column prop="isAttention" label="是否关注" width="120" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isAttention" type="primary" size="small">{{scope.row.isAttention}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" width="300"/>

        <el-table-column label="操作" align="center" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="toAttentionHandler(scope.row)">关注</el-button>
            <el-button size="mini" type="text" @click="toEditHandler(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="studentData.total" 
          :page-size="studentData.pageSize" 
          :current-page="studentData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="50%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form ref="apply_form" :model="form" :rules="rules" size="mini" label-width="80px" label-position="center">
          <div class="sub_title">基本信息</div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="realname">
                <el-input v-model="form.realname"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-radio-group v-model="form.gender">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号" prop="tel">
                <el-input v-model="form.tel"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="qq" prop="qq">
                <el-input v-model="form.qq"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="身份证号" prop="idCard">
                <el-input v-model="form.idCard"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生日" prop="birth">
                <el-date-picker v-model="form.birth" value-format="timestamp" type="date" placeholder="选择日期" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="监护人" prop="guardian">
                <el-input v-model="form.guardian"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="guardianTel">
                <el-input v-model="form.guardianTel"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.homeAddress"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="sub_title">渠道信息</div>
          <el-form-item label="学校渠道" prop="channelId">
            <el-cascader filterable style="width:100%" v-model="form.channelId" clearable :options="channels" placeholder="请选择院校渠道"
        :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item label="毕业时间" prop="graduationTime">
            <el-date-picker v-model="form.graduationTime" value-format="timestamp" type="date" placeholder="选择日期" />
          </el-form-item>
          <el-form-item label="学历" prop="education">
            <el-select v-model="form.education">
              <el-option label="大专" value="大专"></el-option>
              <el-option label="普通本科" value="普通本科"></el-option>
              <el-option label="211本科" value="211本科"></el-option>
              <el-option label="985本科" value="985本科"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="外语水平" prop="flp">
            <el-select v-model="form.flp">
              <el-option value="CET4" label="CET4"></el-option>
              <el-option value="CET6" label="CET6"></el-option>
              <el-option value="CET8" label="CET8"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- /抽屉 -->
  </div>
</template>

<script>
import {get,post,postJSON} from '@/utils/request'
import _ from 'lodash'
import qs from 'qs'
import { mapGetters } from 'vuex'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     loading:false,
     param:{
       page:1,
       pageSize:15
     },
     studentData:{list:[]},
     students:[],
     form:{},
     visible:false,
     title:"编辑学生信息",
     rules: {
        tel: [ { required: true, message: '请输入手机号', trigger: 'blur' } ],
        realname: [ { required: true, message: '请输入姓名', trigger: 'blur' } ],
        qq: [ { required: true, message: '请输入qq', trigger: 'blur' } ],
        gender: [ { required: true, message: '请选择性别', trigger: 'blur' } ],
        channelId:[{ required: true, message: '请选择院校渠道', trigger: 'change' }],
        education:[{ required: true, message: '请选择学历', trigger: 'change' }],
        graduationTime:[{ required: true, message: '请选择毕业时间', trigger: 'change' }]
      },
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['channels'])
  },
  // 侦听器
  watch: {
   
  },
  // 组件方法
  methods: {
    // 去关注
    toAttentionHandler(row){
      this.$prompt('请输入关注理由', '重点关注', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        let url = "/student/attention";
        let data = {
          id:row.id,
          comment:value
        }
        post(url,data).then(resp => {
          this.$message({ message: resp.message, type: 'success' })
          this.loadStudents()
        })
      })
    },
    // 提交学生信息处理函数
    submitHandler() {
      this.$refs['apply_form'].validate((valid) => {
        if (valid) {
          const url = '/student/saveOrUpdate'
          post(url, this.form)
            .then(response => {
              this.visible = false
              this.$message({ message: response.message, type: 'success' })
              this.loadStudents()
            })
        } else {
          return false
        }
      })
    },
    // 去导入学生
    toImportHandler(){
      this.$router.push('/sms/market/train/StudentImport');
    },
    // 去修改
    toEditHandler(row){
      this.form = _.clone(row);
      this.visible = true;
    },
    // 加载学生信息
    loadStudents(){
      this.loading = true;
      let url = '/student/pageQuery'
      let param = _.clone(this.param);
      if(param.channelId){
        param.channelIds = [param.channelId]
      }
      param.stuTypes = ['实训']
      get(url+"?"+qs.stringify(param,{ arrayFormat: 'repeat' }))
      .then(response => {
        this.studentData = response.data
        this.loading = false;
      })
    },
    
     // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadStudents();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadStudents();
    },
    handleSelectionChange(val) {
      this.students = val;
    },
     //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.loadStudents();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>

